package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-03-05
 * Time: 14:26
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        System.out.println("请输入你要删除的图书的名称: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int delIndex = -1;
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)) {
                delIndex = i;
                break;
            }
        }

        if(i == currentSize) {
            System.out.println("没有你找的这本书! ");
            return;
        }

        for (int j = 0; j < currentSize-1; j++) {
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(j,book);
        }

        bookList.setBooks(currentSize,null);

        bookList.setUsedSize(currentSize-1);

    }

}
